package edu.tjhsst.androidIntranet.period8;

import android.app.ExpandableListActivity;
import android.content.ContentUris;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.ExpandableListView;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.AdapterView.OnItemClickListener;
import edu.tjhsst.androidIntranet.R;
import edu.tjhsst.androidIntranet.db.NewsProvider;

public class EighthBlockPicker extends ExpandableListActivity {

	private static final String TAG = "EighthBlockPicker";
	EighthAdapter mainAdapter;
	private View footerView;
	private EighthHandler eighthHandler;

	public void onCreate(Bundle savedInstanceState) {
		//Log.d(TAG, "Booting " + TAG);
		super.onCreate(savedInstanceState);

		requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
		setContentView(R.layout.eighth_block_picker);
		getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.custom_update_title);
		((TextView)findViewById(R.id.title_left_text)).setText("TJ Intranet: Eighth Period");

		ExpandableListView lv = getExpandableListView();
		lv.setScrollBarStyle(ListView.SCROLLBARS_OUTSIDE_INSET);
		LayoutInflater vi = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		footerView = vi.inflate(R.layout.eighth_list_footer, null);
		lv.addFooterView(footerView);
		lv.setOnItemClickListener(new FooterClickListener());

		mainAdapter = new EighthAdapter(this);
		setListAdapter(mainAdapter);
		eighthHandler = new EighthHandler();
		new EighthLoaderThread(eighthHandler, this, false).start();
	}

	public void onListItemClick(ListView l, View v, int position, long id) {

	}

	@Override
	public boolean onChildClick(ExpandableListView parent, View v,
			int groupPosition, int childPosition, long id) {
		//TODO: not done yet, need to actually load identifier
		long identifier = 0;
		Uri launchUri = ContentUris.withAppendedId(Uri.parse("content://" + NewsProvider.NEWS_AUTHORITY + "/newsEntries"), identifier);
		Log.d(TAG, "Launching Uri: " + launchUri.toString());
		startActivity(new Intent(Intent.ACTION_VIEW, launchUri));
		return true;
	}

	private class EighthHandler extends Handler {

		public void handleMessage(Message m) {
			if(m.getData().get("IsUpdateComplete")!=null) {
				TextView mRightText = (TextView) findViewById(R.id.title_right_text);
				ProgressBar mProgressIcon = (ProgressBar) findViewById(R.id.title_progress_icon);
				mRightText.setVisibility(View.INVISIBLE);
				mProgressIcon.setVisibility(View.INVISIBLE);
			} else {
				//mainAdapter.notifyDataSetChanged();
				EighthBlockPicker.this.onContentChanged();
			}
		}

	}

	public class FooterClickListener implements OnItemClickListener {

		@Override
		public void onItemClick(AdapterView<?> parent, View view, int position,
				long id) {
			if(view==footerView) {
				Log.d(TAG, "Loading Next Week");
				new EighthLoaderThread(eighthHandler, EighthBlockPicker.this, true).start();
				mainAdapter.loadNextTwoWeeks();
			}

		}

	}

}
